// 权限对应map   v-permission="['Administration']"  -> 表示仅行政才显示
/*
  应用设置权限   ApplicationSetting
  APP设置权限    AppSetting
  档案权限       Archives
  日程权限       Schedule
  项目权限       Project
  销售           Sale
  教务          EducationalAdministration
  学籍          StudentStatus
  客服          CustomerService
  车管          VehicleManagement
  行政          Administration
  财务          Finance
  人事          Personnel
  权限          /ums
*/

function checkPermission(el, binding) {
    const { value } = binding;
    if (!value || !(value instanceof Array)) throw new Error(`need roles! Like v-permission="['role1','role2']"`);

    let roles = JSON.parse(localStorage.getItem("role")) || {};
    // roles = ["Personnel", "Finance"];
    let currentRoles = Object.keys(roles).map((v) => roles[v].name);
    // 判断有没有这个部门
    let hasPermission = value.some((v) => currentRoles.includes(v));
    // let hasPermission = value.some((v) => roles.includes(v));
    if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el);
    }
}

export default {
    name: "permission",
    inserted(el, binding) {
        checkPermission(el, binding);
    },
    update(el, binding) {
        checkPermission(el, binding);
    },
};
